package onlinejudge;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by yzy on 2020-02-16 10:46
 */
public class ThreadJudge {

    static Lock lock = new ReentrantLock();
    static Condition con = lock.newCondition();
    static String str = "This is a Test";
    volatile static int idx = 0;
    volatile static int threadNum = 0;

    public static void main(String[] args) {
        TestThread t0 = new TestThread(0,1);
        TestThread t1 = new TestThread(1,2);
        TestThread t2 = new TestThread(2,0);

        t2.start();
        t1.start();
        t0.start();


    }


    static class TestThread extends Thread{

        private int currTheadNum;
        private int nextThreadNum;

        public TestThread(int currTheadNum, int nextThreadNum) {
            this.currTheadNum = currTheadNum;
            this.nextThreadNum = nextThreadNum;
        }

        @Override
        public void run() {
            lock.lock();
            if(currTheadNum == currTheadNum){
                System.out.println(str.charAt(idx++));
                con.signalAll();
            }else{
                try {
                    con.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
